<template>
  <div class="user-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('closePop')"
      @confirm="onFinish"
    />
  </div>
</template>

<script>
import { editUserProfile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  name: 'UserBirthday',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1960, 0, 1),
      maxDate: new Date(2021, 10, 1),
      currentDate: new Date(this.value),
      message: this.value
    }
  },
  methods: {
    async onFinish () {
      this.message = dayjs(this.currentDate).format('YYYY-MM-DD')
      try {
        await editUserProfile({
          birthday: this.message
        })
        // 更新视图 关闭弹层 提示成功
        this.$emit('input', this.message)
        this.$emit('closePop')
        this.$toast.success('修改成功')
      } catch (err) {
        this.$toast('修改失败，稍后重试')
      }
    }
  }
}
</script>

<style>

</style>
